package com.example.administrator.familfinalcialmanagement.ui.activity;

import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.view.View;

import com.example.administrator.familfinalcialmanagement.R;
import com.example.administrator.familfinalcialmanagement.bean.GreenDaoUtils;
import com.example.administrator.familfinalcialmanagement.bean.JaveTeacher;
import com.example.administrator.familfinalcialmanagement.databinding.ActivityTeacherDescBinding;
import com.example.administrator.familfinalcialmanagement.greendao.gen.JaveTeacherDao;
import com.example.administrator.familfinalcialmanagement.ui.BaseActivity;
import com.example.administrator.familfinalcialmanagement.util.LogUtil;
import com.example.administrator.familfinalcialmanagement.util.SpUtils;
import com.example.administrator.familfinalcialmanagement.util.ToastUtil;

public class TeacherDescActivity extends BaseActivity {

    ActivityTeacherDescBinding binding;
    JaveTeacherDao teacherDao = GreenDaoUtils.getSingleTon().getmDaoSession().getJaveTeacherDao();
    JaveTeacher javeTeacher;

    String isLogin, pwdStatus;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_teacher_desc);
        binding.setActivity(this);
        initView();
    }

    /**
     * 1 管理员
     * 2 科目管理者
     */
    private void initView() {
        isLogin = (String) SpUtils.get("isLogin", "");
        LogUtil.e("isLogin-->" + isLogin);

        String intentPhone = getIntent().getStringExtra("module_phone");
        String intentPwd = getIntent().getStringExtra("module_pwd");
        for (JaveTeacher teacher : teacherDao.loadAll()) {
            if (intentPhone.equals(teacher.getPhone()) && intentPwd.equals(teacher.getPwd())) {
                binding.setBean(teacher);
                javeTeacher = teacher;
                pwdStatus = javeTeacher.getOne();
            }
        }

        /**
         *  缓存中存储：
         *  isLogin   1 超级管理员在登录的状态
         *              2 科目管理员登录的状态
         *                  3 普通用户登录的状态
         *
         *   teacher.getOne()  1  密码锁定  2 密码非锁定
         */
        if ("1".equals(isLogin)) {
            LogUtil.e("管理员");
            binding.etModulePwdStatus.setOnClickListener(this);
        } else {
            LogUtil.e("科目管理者");
        }

        if ("1".equals(pwdStatus)) {
            binding.etModulePwdStatus.setText("锁定");
        } else {
            binding.etModulePwdStatus.setText("非锁定");
        }

        binding.tvModuleUp.setOnClickListener(this);
        binding.etModulePwdStatus.setOnClickListener(this);
    }

    boolean isPoint = true;

    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.etModulePwdStatus:
                if (isPoint) {
                    isPoint = false;
                    if ("1".equals(pwdStatus)) {
                        pwdStatus = "2";
                        binding.etModulePwdStatus.setText("非锁定");
                    } else {
                        pwdStatus = "1";
                        binding.etModulePwdStatus.setText("锁定");
                    }
                } else {
                    isPoint = true;
                    if ("1".equals(pwdStatus)) {
                        pwdStatus = "2";
                        binding.etModulePwdStatus.setText("非锁定");
                    } else {
                        pwdStatus = "1";
                        binding.etModulePwdStatus.setText("锁定");
                    }
                }
                break;
            case R.id.tvModuleUp:
                String name = binding.etModuleName.getText().toString().trim();
                if (TextUtils.isEmpty(name)) {
                    ToastUtil.showToast("请输入名字");
                    return;
                }

                String className = binding.etModuleClassName.getText().toString().trim();
                if (TextUtils.isEmpty(className)) {
                    ToastUtil.showToast("请输入课程");
                    return;
                }

                String phone = binding.etModulePhone.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.showToast("请输入账号");
                    return;
                }

                String pwd;
                if ("1".equals(isLogin)) {
                    pwd = binding.etModulePwd.getText().toString().trim();
                    if (TextUtils.isEmpty(pwd)) {
                        ToastUtil.showToast("请输入密码");
                        return;
                    }
                } else {
                    LogUtil.e("科目管理者");
                    if ("2".equals(isLogin) && javeTeacher.getOne().equals("2")) {
                        pwd = binding.etModulePwd.getText().toString().trim();
                        if (TextUtils.isEmpty(pwd)) {
                            ToastUtil.showToast("请输入密码");
                            return;
                        }
                    } else {
                        pwd = javeTeacher.getPwd();
                    }
                }

                /**
                 * 根据指定属性查找更新
                 */
                JaveTeacher teacher = teacherDao.queryBuilder().where(JaveTeacherDao.Properties.Phone.eq(phone)).build().unique();
                teacher.setName(name);
                teacher.setClassName(className);
                teacher.setPhone(phone);
                teacher.setPwd(pwd);
                teacher.setOne(pwdStatus);
                teacherDao.update(teacher);
                ToastUtil.showToast("修改完毕");
                finish();
                break;
            default:
        }
    }
}
